﻿using Gp.Scripts.Data;
using Gp.Scripts.Core;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;

namespace Gp.Scripts.Core {
    /// 存储Unit的属性
    // [CreateAssetMenu(fileName = "UnitStat", menuName = "Gp/单位数值", order = 0)]
    public class UnitPrefabEntityConfig : EntityEntityConfig {
# if UNITY_EDITOR
        [ShowIf("@flag")]
        [Title("模型")]
        [HorizontalGroup("数据", 200), VerticalGroup("数据/preview")]
        [PreviewField(100, ObjectFieldAlignment.Center)]
        [PropertyOrder(-99)]
        [SerializeField]
        [OnValueChanged("OnModelChanged")]
        [HideLabel]
        private GameObject modelDisplay;
#endif
        [HideInInspector] public UnitModel model;

        [ShowIf("@!flag")]
        [Title("头像")]
        [HorizontalGroup("数据", 200), VerticalGroup("数据/preview")]
        [PreviewField(100, ObjectFieldAlignment.Center)]
        [HideLabel]
        public Sprite avatar;


        [VerticalGroup("数据/preview"), HideLabel]
        public string unitName = "new Unit";
        
        [VerticalGroup("数据/preview"), HideLabel]
        public EntityType unitType = EntityType.Humanoid;
        [TextArea(10, 30)] [VerticalGroup("数据/preview")] [LabelText("单位描述"), Space(5)]
        public string description = "this is a new Unit";

        public override EntityType EntityType => unitType;
        // public GameObject boneAsh => LevelObjectConfigDatabase.Instance.boneAsh;
        
        
#if UNITY_EDITOR
        [SerializeField, HideInInspector] private bool flag = false;
        [Button("切换预览", ButtonStyle.CompactBox)]
        private void Switch() {
            flag = !flag;
        }
#endif

        [LabelText("单位AI")]
        public UnitAIBrainWrapper aiBrainPreset;
        

        // [LabelText("人类单位")]
        // [Tooltip("人类单位最大生命值为 10")]
        // public bool isHuman = true;
        // [HideIf("isHuman"), ProgressBar(10, 200)]
        // [LabelText("最大生命值")]
        // public int maxHealth = 10;
        
        
        [VerticalGroup("数据/status")]
        [TabGroup("数据/status/data", tab:"数值配置")]
        [HideLabel]
        public CharacterStatConfig config;
        

        [VerticalGroup("数据/status")] [TabGroup("数据/status/data", tab: "职业配置")]
        [HideLabel]
        public UnitProfessionData profession;
        
        [VerticalGroup("数据/status")]
        [TabGroup("数据/status/data", tab:"装备配置")]
        public Data.UnitEquip equip;

        [VerticalGroup("数据/status")]
        [TabGroup("数据/status/data", tab:"背包库存")]
        [HideLabel]
        [OdinSerialize]
        [HideReferenceObjectPicker]
        public UnitInventory inventory = new();
        

# if UNITY_EDITOR
        private void OnModelChanged() {
            if (modelDisplay == null) {
                return;
            }
            if (modelDisplay.TryGetComponent<UnitModel>(out var unitModel)) {
                model = unitModel;
            }
            else {
                modelDisplay = null;
                Debug.LogWarning("Not available object. <UnitModel> Component  is needed.");
            }
        }
# endif

        
        
        public override string ItemName {
            get => this.unitName;
            set => this.unitName = value;
        }

        public override string Description {
            get => this.description;
            set => this.description = value;
        }

        public override Sprite Icon {
            get => this.avatar;
            set => this.avatar = value;
        }
    }
}